<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <link rel="stylesheet" href="styles/style.css">
        <link rel="stylesheet" href="styles/theme/atom-one-dark.css">
        <title>例子7-JS进阶（二）</title>
    </head>
    <body class="cols">
        <pre>
            <code>
                    // 例子7

                    var a = 20;

                    function fn() {
                        console.log(this.a);
                    }
                    fn(); // 20, 函数fn独立调用，this为window
            </code>
        </pre>
        <pre>
            <code>
                    var b = 20;

                    function bar() {
                        function foo() {
                            console.log(this.b);
                        }
                        foo(); // 函数foo独立调用，this为window
                    }
                    bar(); // 20
            </code>
        </pre>
        <pre>
            <code>
                    var c = 20;
                    var obj = {
                        c: 10,
                        d: this.c + 20,
                        baz: function () {
                            return this.c;
                        }
                    }
                    console.log(obj.d); // 40, this直接指向window
                    console.log(obj.baz()); // 10, 函数baz被obj对象拥有，this指向对象obj

            </code>
        </pre>
        <pre>
            <code>
                    function spam() {
                        'use strict';
                        console.log(this); // 由于设置了严格模式，this指向undefined
                    }

                    spam(); // 函数spam独立调用
                    window.spam(); // 函数spam被window所拥有，this指向了window
            </code>
        </pre>
        <script src="scripts/libs/highlight.pack.js"></script>
        <script src="scripts/7.js"></script>
        <script>
            hljs.initHighlightingOnLoad();
        </script>
    </body>
</html>